﻿(function() {

  define(['jquery', 'use!underscore', 'use!backbone', 'namespace', 'photo', 'album', 'photoView', 'text!templates/show_photo.html', 'text!templates/edit_photo.html', 'jquery_ui'], function($, _, Backbone, global, Photo, Album, PhotoView, ShowPhotoTemplate, EditPhotoTemplate) {
    var AddView;
    return AddView = Backbone.View.extend({
      tagName: 'figure',
      attributes: {
        "class": 'photo',
        id: 'add'
      },
      templateFunc: {
        show: _.template(ShowPhotoTemplate),
        edit: _.template(EditPhotoTemplate)
      },
      render: function(template) {
        this.$el.html(this.templateFunc[template](new Photo().toJSON()));
        _.each($('.datepicker'), function(input) {
          return $(input).datepicker({
            dateFormat: 'yy-mm-dd',
            currentText: input.value
          });
        });
        return this;
      },
      events: {
        'click #cancel': 'cancelAdd',
        'click #save': 'addPhoto'
      },
      cancelAdd: function(e) {
        e.preventDefault();
        this.remove();
        return global.app.router.navigate('/');
      },
      addPhoto: function() {
        var formData, newModel, photo;
        formData = {};
        this.$el.find('input').each(function() {
          var $input;
          $input = $(this);
          return formData[$input.attr('id')] = $input.val();
        });
        newModel = new Photo(formData);
        newModel.set('type', $('#album :selected').val(), {
          silent: true
        });
        Album.url = '/Photo/';
        Album.create(newModel, {
          wait: true
        });
        this.remove();
        photo = new PhotoView({
          model: newModel
        });
        $('.photo:first').before(photo.render('show').el);
        return global.app.router.navigate('/');
      }
    });
  });

}).call(this);
